import { stringify } from 'qs';
import { downloadArrayBufferFile, getMd5, getToken, request } from '@/utils';
import { message } from 'antd';
import moment from 'moment'
import { fingerprintFetch } from '@/utils/request';

export const retrievalKeyword = async (keyword: string) => {
  const result = await request('/feedback/logSearch/definedKeyword?keyword=' + keyword);

  return result;
};

export const fetchDefinedData = async (params: any) => {
  if (params.rangeTime) {
    params.startDate = params.rangeTime[0].format('YYYY-MM-DD');
    params.endDate = params.rangeTime[1].format('YYYY-MM-DD');
  }
  delete params.rangeTime;

  const result = await request('/feedback/logSearch/defined/page?' + stringify(params));

  return result.data;
};

export const fetchUndefinedData = async (params: any) => {
  if (params.rangeTime) {
    params.startDate = params.rangeTime[0].format('YYYY-MM-DD');
    params.endDate = params.rangeTime[1].format('YYYY-MM-DD');
  }
  if (params.createTime) {
    params.createDateStart = params.createTime[0].format('YYYY-MM-DD');
    params.createDateEnd = params.createTime[1].format('YYYY-MM-DD');
  }

  delete params.rangeTime;
  delete params.createTime;

  const result = await request('/feedback/logSearch/undefined/page?' + stringify(params));

  return result.data;
};

export const createUndefined = async (params: any) => {
  if (params.rangeTime) {
    params.startDate = params.rangeTime[0].format('YYYY-MM-DD');
    params.endDate = params.rangeTime[1].format('YYYY-MM-DD');
  }
  delete params.rangeTime;

  const result = await request('/feedback/logSearch/undefined/task/create', {
    method: 'POST',
    data: params,
  });

  return result;
};

export const fetchUndefinedDetail = async (query: any) => {
  const result = await request('/feedback/logSearch/undefined/details?' + stringify(query));

  return {
    list: result.data?.list || [],
    total: result.data?.pagination?.total || 0,
  };
};

export const fetchFileUrl = async (fileUuid: string) => {
  const url = `${window.g.url}/presign/downloadurl`;

  const result = await (
    await fingerprintFetch(url, {
      method: 'POST',
      body: JSON.stringify({
        fileUuid,
      }),
      headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        OptAuthorization: `${getToken()}`,
        shaSign: getMd5(
          JSON.stringify({
            fileUuid,
          }),
        ),
      },
    })
  ).json();

  if (result?.data?.presignDownloadUrl) {
    window.open(result.data.presignDownloadUrl, '_self');
  }
};

export const stopTask = async (id: string) => {
  const result = await request('/feedback/logSearch/undefined/task/terminate?id=' + id, {
    method: 'POST',
    // data: { id },
  });

  return result;
};

export const exportList = async (params: any) => {
  if (params.rangeTime) {
    params.startDate = params.rangeTime[0].format('YYYY-MM-DD');
    params.endDate = params.rangeTime[1].format('YYYY-MM-DD');
    delete params.rangeTime;
  }

  const json = JSON.stringify(params);
  const url = `${window.g.url}/feedback/logSearch/defined/export`;

  fingerprintFetch(url, {
    method: 'POST',
    body: json,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      OptAuthorization: `${getToken()}`,
      shaSign: getMd5(json),
    },
  })
    .then(async response => response.blob()) // 转换响应为blob
    .then((blob: any) => {
      if (!blob.size) {
        message.error('当前条件下，暂无数据');
        return;
      }

      downloadArrayBufferFile(blob, `${moment().format('YYYYMMDDHHmmss')}搜索列表.xlsx`);
    });
};
